package vip.xumy.admin.verify.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import vip.xumy.admin.verify.pojo.User;
import vip.xumy.admin.verify.service.UserService;
import vip.xumy.core.pojo.com.AjaxResponse;

/**
 * @author:mengyxu
 * @date:2020年10月28日
 */

@RestController
@RequestMapping("public")
public class PublicController {
	public final static String KEY = "user";
	@Autowired
	private UserService userService;

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public AjaxResponse login(@RequestBody User param, HttpServletRequest request) {
		User user = userService.login(param);
		if (user == null) {
			return new AjaxResponse(false, "登录失败，用户名或密码错误");
		}
		request.getSession().setAttribute(KEY, user);
		return new AjaxResponse(true, "登录成功");
	}

	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public AjaxResponse logout(HttpServletRequest request) {
		request.getSession().removeAttribute(KEY);
		return new AjaxResponse(true, "登出成功");
	}

	@RequestMapping(value = "info", method = RequestMethod.GET)
	public User login(HttpServletRequest request) {
		Object user = request.getSession().getAttribute(KEY);
		if (user == null) {
			return null;
		}
		return (User) user;
	}

}
